////*MAIN ANALYSIS WITH MACRO DATA*///////

use "MACRO-econ-elections.dta", clear

/*create/recode variables for main analysis*/
xtset ccode timevar
gen dedate = d.edate
gen period01 = periods
recode period01 1=0 2/max=1
label variable period01 "incumbent previous period"
label define previous 0 "different" 1 "same" 
label values period01 previous
rename inexpty_t inexpty_told
rename inexpty_t1 inexpty_t1old
gen inexpty_t = 100 - execpty_t - moppty_t
gen inexpty_t1 = 100 - execpty_t1 - moppty_t1
gen nondemoc = 0
recode nondemoc *=1 if year==1979 & country=="Spain"
recode nondemoc *=1 if year==1977 & country=="Greece"
recode nondemoc *=1 if year==1976 & country=="Portugal"
recode nondemoc *=1 if country=="Turkey"
gen epHAg = ephagrowth22
gen lepHAg = l.epHAg
gen r_epHAg = epHAg*-1
gen r_lepHAg = lepHAg*-1
/*drop cases w/caretaker govts*/
list ccode country year timevar if caretaker==1
drop if caretaker==1
/*making outlier measure for excluding epHAg clear outlier in Greece*/
gen outlier = 0
recode outlier *=1 if ccode==11 & year==2012
/*Incumbent classification used in main analysis*/
gen govpty2_t = execpty_t + jrptnr_t
gen govpty2_t1 = execpty_t1 + jrptnr_t1
rename inexpty2_t inexpty2_told
rename inexpty2_t1 inexpty2_t1old
gen inexpty2_t = 100 - govpty2_t - moppty_t
gen inexpty2_t1 = 100 - govpty2_t1 - moppty_t1

*************************************************
*****ANALYSIS APPEARING IN MANUSCRIPT
*************************************************
/*****TABLE 5 results********/
sureg (govpty2_t epHAg lepHAg period01 govpty2_t1 i.ccode) /// 
	(moppty_t epHAg lepHAg period01 moppty_t1 i.ccode) /// 
	(inexpty2_t epHAg lepHAg period01 inexpty2_t1 i.ccode) if outlier==0 & nondemoc==0

sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 i.ccode) if outlier==0 & nondemoc==0


/******FIGURE 4 results*********/
qui sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 i.ccode) if outlier==0 & nondemoc==0

qui margins, at(epHAg=0 lepHAg=0 period01=(0 1)) predict(equation(#1))
marginsplot, scheme(s1mono) ytitle("Expected vote for incumbent") leg(off) title("A. Incumbent") name(d1, replace) nodraw
qui margins, at(epHAg=0 lepHAg=0 period01=(0 1)) predict(equation(#2))
marginsplot, scheme(s1mono) ytitle("Expected vote for alternative") leg(off) title("B. Alternative") name(e1, replace) nodraw
qui margins, at(epHAg=0 lepHAg=0 period01=(0 1)) predict(equation(#3))
marginsplot, scheme(s1mono) ytitle("Expected vote for challenger") leg(off) title("C. Challenger") name(f1, replace) nodraw
graph combine d1 e1 f1, scheme(s1mono) ycommon r(1)



**********************************************************
*****ANALYSES APPEARING IN SUPPLEMENTARY INFORMATION FILE
**********************************************************

/*TABLE A4 junior coalition partners included with inexperienced parties*/
sureg (execpty_t epHAg c.lepHAg##c.period01 execpty_t1 i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 i.ccode) /// 
	(inexpty_t epHAg c.lepHAg##c.period01 inexpty_t1 i.ccode) if outlier==0 & nondemoc==0


/*TABLE A5*/	
////*no country fixed effects*///
sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1) if outlier==0 & nondemoc==0
predict e if e(sample)

///*OLS*///
reg govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 i.ccode if e~=.
reg moppty_t epHAg c.lepHAg##c.period01 moppty_t1 i.ccode if e~=.
reg inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 i.ccode if e~=.


/***TABLE A6 AND FIGURE A4***/ 
gen period123 = periods
recode period123 1=1 2=2 3/max=3
label variable period123 "inc periods in office"
sureg (govpty2_t epHAg c.lepHAg##period123 govpty2_t1 i.ccode) /// 
	(moppty_t epHAg c.lepHAg##period123 moppty_t1 i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##period123 inexpty2_t1 i.ccode) if outlier==0 & nondemoc==0
	qui margins, at(epHAg=0 lepHAg=0 period123=(1(1)3)) predict(equation(#1))
marginsplot, scheme(s1mono) ytitle("Expected vote for incumbent") leg(off) title("A. Incumbent") name(g1, replace) nodraw
qui margins, at(epHAg=0 lepHAg=0 period123=(1(1)3)) predict(equation(#2))
marginsplot, scheme(s1mono) ytitle("Expected vote for alternative") leg(off) title("B. Alternative") name(h1, replace) nodraw
qui margins, at(epHAg=0 lepHAg=0 period123=(1(1)3)) predict(equation(#3))
marginsplot, scheme(s1mono) ytitle("Expected vote for challenger") leg(off) title("C. Challenger") name(i1, replace) nodraw
graph combine g1 h1 i1, scheme(s1mono) ycommon r(1) 
 
 
/////*Bringing in election level rile and median voter variables from comparative manifesto data*/
merge 1:1 ccode edate using "election_rile.dta", force
xtset ccode timevar

/****SUPPLY SIDE ARGUMENTS**********/

/*****TABLE A7*******/
/*incumbent distance*/
gen inc_drift = abs(inc_rile - median_voter)
sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 inc_drift i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 inc_drift i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 inc_drift i.ccode) ///
	if outlier==0 & nondemoc==0, small
predict e1 if e(sample)
estimates store full1

/*resticted without incumbent distance*/	
sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 i.ccode) if e1~=., small
lrtest full1, stats	

/*re-run full with incumbent distance and small sample statistics for wald tests of significance*/
sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 inc_drift i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 inc_drift i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 inc_drift i.ccode) if outlier==0 & nondemoc==0, small

test [govpty2_t]inc_drift
test [moppty_t]inc_drift
test [inexpty2_t]inc_drift

/*polarization*/
gen lpolarization = l.rile_polarization
sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 lpolarization i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 lpolarization i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 lpolarization i.ccode) ///
	if outlier==0 & nondemoc==0, small
predict e2 if e(sample)	
estimates store full2

test [govpty2_t]lpolarization
test [moppty_t]lpolarization
test [inexpty2_t]lpolarization

/*enep*/
sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 eff_nr_parties i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 eff_nr_parties i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 eff_nr_parties i.ccode) ///
	if outlier==0 & nondemoc==0, small
predict e3 if e(sample)	
estimates store full3
	
test [govpty2_t]eff_nr_parties
test [moppty_t]eff_nr_parties
test [inexpty2_t]eff_nr_parties

/****TABLE A8****/
/*SUR controlling for issue emphasis heterogeneity*/
sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 heterogeneity i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 heterogeneity i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 heterogeneity i.ccode) ///
	if outlier==0 & nondemoc==0, small
predict e4 if e(sample)	
estimates store full4

test [govpty2_t]heterogeneity
test [moppty_t]heterogeneity
test [inexpty2_t]heterogeneity

/*restricted without issue heterogeneity*/	
sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 i.ccode) if e4~=., small
lrtest full4, stats	

/*re-run full with heterogeneity and small sample statistics for wald tests of significance*/
sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 heterogeneity i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 heterogeneity i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 heterogeneity i.ccode) ///
	if outlier==0 & nondemoc==0, small	
test heterogeneity

/*specific issues*/
gen envdiff = chl_env - (inc_env + opp_env)/2
gen immdiff = chl_imm - (inc_imm + opp_imm)/2
gen eurdiff = chl_eur - (inc_eur + opp_eur)/2	
gen rhtdiff = chl_rht - (inc_rht + opp_rht)/2
gen eurcomb = chl_eur + inc_eur + opp_eur

/*specific issues: environment*/
sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 inc_env i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 opp_env i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 chl_env i.ccode) ///
	if outlier==0 & nondemoc==0 & ccode~=1 & ccode~=4 & ccode~=15 & ccode~=19, small
predict e5 if e(sample)	

test [govpty2_t]inc_env
test [moppty_t]opp_env
test [inexpty2_t]chl_env
	
sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 inc_env i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 opp_env i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 chl_env i.ccode) ///
	if outlier==0 & nondemoc==0 & ccode~=1 & ccode~=4 & ccode~=15 & ccode~=19, small
	
/*specific issues: immigration*/	
	sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 inc_imm i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 opp_imm i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 chl_imm i.ccode) ///
	if outlier==0 & nondemoc==0 & ccode~=1 & ccode~=4 & ccode~=15 & ccode~=19, small
	predict e6 if e(sample)	

test [govpty2_t]inc_imm
test [moppty_t]opp_imm
test [inexpty2_t]chl_imm

/****TABLE A9*****/
/*vdem antipluralism*/
sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 v2xpa_antiplural_e i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 v2xpa_antiplural_m i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 v2xpa_antiplural_i i.ccode) ///
	if outlier==0 & nondemoc==0 & ccode~=1 & ccode~=4 & ccode~=15 & ccode~=19, small
		predict e7 if e(sample)	

test [govpty2_t]v2xpa_antiplural_e
test [moppty_t]v2xpa_antiplural_m
test [inexpty2_t]v2xpa_antiplural_i
	
/*vdem populism*/
sureg (govpty2_t epHAg c.lepHAg##c.period01 govpty2_t1 v2xpa_popul_e i.ccode) /// 
	(moppty_t epHAg c.lepHAg##c.period01 moppty_t1 v2xpa_popul_m i.ccode) /// 
	(inexpty2_t epHAg c.lepHAg##c.period01 inexpty2_t1 v2xpa_popul_i i.ccode) ///
	if outlier==0 & nondemoc==0 & ccode~=1 & ccode~=4 & ccode~=15 & ccode~=19, small
predict e8 if e(sample)	

test [govpty2_t]v2xpa_popul_e
test [moppty_t]v2xpa_popul_m
test [inexpty2_t]v2xpa_popul_i
